/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package campussmartsecurity;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author brlnt
 */
public class ManajemenAnggota extends javax.swing.JFrame {

    /**
     * Creates new form ManajemenAnggota
     */
    String sql;
    Konek k = new Konek();
    
    public ManajemenAnggota() {
        initComponents();
        viewAnggota(null, null);
        setLocationRelativeTo(null);
    }
    
    private String userData[] = new String[3];
    public ManajemenAnggota(String input[]){
        initComponents();
        userData = input;
        viewAnggota(null, null);
        
    }
    /**
     * Manajemen Anggota
     */
    void viewAnggota(String loc, String query){
        ArrayList<String> hasil = new ArrayList<String>();
        sql= "SELECT id_skk, nama_skk FROM `skk`";
        if (loc != null && query != null){
            if(loc.equals("id")){
                sql += " WHERE id_skk = "+query+";";
            }else{
                sql += " WHERE nama_skk='"+query+"'";
            }
        }
        System.out.println(sql);
        try{
            k.rs = k.stmt.executeQuery(sql);
            while(k.rs.next()){
                hasil.add(k.rs.getString(1));
                hasil.add(k.rs.getString(2));
            }
            String tblhasil[][] = new String[hasil.size()/2][2];
            int count=0;
            for (int i = 0; i < hasil.size()/2; i++) {
                for (int j = 0; j < 2; j++) {
                    tblhasil[i][j] = hasil.get(count);
                    count++;
                }
            }
            String kolom[] = {"ID", "Nama"};
            tabelAnggota.setModel(new javax.swing.table.DefaultTableModel(tblhasil, kolom));
            labelStatus.setText("Query success");
            tabelAnggota.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    
                }
            });
        }
        catch(Exception e){
            System.out.println(e);
            labelStatus.setText("Query error");
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelBuatUser = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        usernameSKK = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        fieldPass1 = new javax.swing.JPasswordField();
        jLabel16 = new javax.swing.JLabel();
        fieldPass2 = new javax.swing.JPasswordField();
        jButton9 = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        panelDaftarAnggota = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tabelAnggota = new javax.swing.JTable();
        comboQueryMA = new javax.swing.JComboBox();
        fieldQueryMA = new javax.swing.JTextField();
        buttonSearchDaftarAnggota = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel17 = new javax.swing.JLabel();
        labelStatus = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Manajemen Anggota");
        setResizable(false);

        panelBuatUser.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tambah Anggota", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel5.setText("Nama");

        jLabel6.setText("Password");

        fieldPass1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldPass1ActionPerformed(evt);
            }
        });

        jLabel16.setText("Confirm Password");

        jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/add.png"))); // NOI18N
        jButton9.setText("Tambah Anggota");
        jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton9MouseClicked(evt);
            }
        });

        jLabel18.setForeground(new java.awt.Color(255, 0, 0));

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Anggota" }));

        jLabel19.setText("Kewenangan");

        javax.swing.GroupLayout panelBuatUserLayout = new javax.swing.GroupLayout(panelBuatUser);
        panelBuatUser.setLayout(panelBuatUserLayout);
        panelBuatUserLayout.setHorizontalGroup(
            panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBuatUserLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelBuatUserLayout.createSequentialGroup()
                        .addComponent(jLabel18, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton9))
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(panelBuatUserLayout.createSequentialGroup()
                        .addGroup(panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel19)
                            .addGroup(panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel5)
                                .addComponent(jLabel6)
                                .addComponent(jLabel16)
                                .addComponent(fieldPass1)
                                .addComponent(usernameSKK)
                                .addComponent(fieldPass2, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelBuatUserLayout.setVerticalGroup(
            panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBuatUserLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usernameSKK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldPass1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel16)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fieldPass2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel19)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelBuatUserLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton9))
                .addGap(66, 66, 66))
        );

        panelDaftarAnggota.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Manajemen Anggota", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        tabelAnggota.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "ID", "Username"
            }
        ));
        tabelAnggota.setFillsViewportHeight(true);
        tabelAnggota.getTableHeader().setReorderingAllowed(false);
        jScrollPane4.setViewportView(tabelAnggota);

        comboQueryMA.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "id", "nama_skk" }));
        comboQueryMA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboQueryMAActionPerformed(evt);
            }
        });

        buttonSearchDaftarAnggota.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/zoom.png"))); // NOI18N
        buttonSearchDaftarAnggota.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonSearchDaftarAnggotaMouseClicked(evt);
            }
        });
        buttonSearchDaftarAnggota.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSearchDaftarAnggotaActionPerformed(evt);
            }
        });

        jButton10.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/delete.png"))); // NOI18N
        jButton10.setText("Hapus Anggota");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        jButton11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/pencil.png"))); // NOI18N
        jButton11.setText("Edit");

        jLabel17.setText("Cari Anggota :");

        javax.swing.GroupLayout panelDaftarAnggotaLayout = new javax.swing.GroupLayout(panelDaftarAnggota);
        panelDaftarAnggota.setLayout(panelDaftarAnggotaLayout);
        panelDaftarAnggotaLayout.setHorizontalGroup(
            panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                        .addComponent(jButton11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton10))
                    .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                        .addGroup(panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel17)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                                .addComponent(comboQueryMA, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fieldQueryMA, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonSearchDaftarAnggota, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 1, Short.MAX_VALUE)))
                .addContainerGap())
        );
        panelDaftarAnggotaLayout.setVerticalGroup(
            panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelDaftarAnggotaLayout.createSequentialGroup()
                        .addGroup(panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboQueryMA, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fieldQueryMA, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(7, 7, 7)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelDaftarAnggotaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton10)
                            .addComponent(jButton11)))
                    .addComponent(buttonSearchDaftarAnggota))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        labelStatus.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(labelStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(panelDaftarAnggota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelBuatUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelBuatUser, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panelDaftarAnggota, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboQueryMAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboQueryMAActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_comboQueryMAActionPerformed

    private void buttonSearchDaftarAnggotaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonSearchDaftarAnggotaMouseClicked
        // TODO add your handling code here:
        viewAnggota(comboQueryMA.getSelectedItem().toString(), fieldQueryMA.getText());
    }//GEN-LAST:event_buttonSearchDaftarAnggotaMouseClicked

    private void buttonSearchDaftarAnggotaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSearchDaftarAnggotaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_buttonSearchDaftarAnggotaActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton10ActionPerformed

    private void fieldPass1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldPass1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fieldPass1ActionPerformed

    private void jButton9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton9MouseClicked
        // TODO add your handling code here:
        String pass1 = new String(fieldPass1.getPassword());
        String pass2 = new String(fieldPass2.getPassword());
        if(usernameSKK.getText().equals("")){
            jLabel18.setText("*Isi semua kolom");
        }else if (!pass1.equals(pass2) || pass1.equals("")){
            jLabel18.setText("*Password tidak cocok");
        }else {
            int kewenangan = jComboBox1.getSelectedIndex()+1;
            sql = "INSERT INTO `skk` (`NAMA_SKK`, `PASSWORD_SKK`, `KEWENANGAN_SKK`) VALUES ('"+usernameSKK.getText()+"',md5('"+pass1+"'),'"+String.valueOf(kewenangan)+"')";
            System.out.println(sql);
            try{
                k.stmt.executeUpdate(sql);
                JOptionPane.showMessageDialog(null ,"Penambahan Anggota SKK berhasil", "Selamat", JOptionPane.INFORMATION_MESSAGE);
                viewAnggota(null, null);
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }//GEN-LAST:event_jButton9MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ManajemenAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ManajemenAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ManajemenAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ManajemenAnggota.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ManajemenAnggota().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSearchDaftarAnggota;
    private javax.swing.JComboBox comboQueryMA;
    private javax.swing.JPasswordField fieldPass1;
    private javax.swing.JPasswordField fieldPass2;
    private javax.swing.JTextField fieldQueryMA;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton9;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelStatus;
    private javax.swing.JPanel panelBuatUser;
    private javax.swing.JPanel panelDaftarAnggota;
    private javax.swing.JTable tabelAnggota;
    private javax.swing.JTextField usernameSKK;
    // End of variables declaration//GEN-END:variables

    /**
     * @param userData the userData to set
     */
    public void setUserData(String[] userData) {
        this.userData = userData;
    }
}
